package cn.zhz.service;

import cn.zhz.dao.HibernateUtil;
import cn.zhz.dao.UserDao;
import cn.zhz.pojo.User;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;

import java.util.List;

public class UserService {
    private UserDao userDao = new UserDao();

    public User findUserById(int id){
        Transaction tx=null;
        User result=null;

        try {
            tx = HibernateUtil.currentSession().beginTransaction();
            result = userDao.get(id);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }
        return result;
    }

    public void addNewUser(User user){
        Transaction tx=null;
        try {
            tx=userDao.currentSession().beginTransaction();
            userDao.save(user);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
    }

    public void updateUser(User user){
        Transaction tx=null;
        try {
            tx=userDao.currentSession().beginTransaction();
            User userToUpdate = userDao.load(user.getId());
            userToUpdate.setName(user.getName());
            userToUpdate.setPassword(user.getPassword());
            userToUpdate.setTelephone(user.getTelephone());
            userToUpdate.setUserName(user.getUserName());
            userToUpdate.setIsAdmin(user.getIsAdmin());
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx != null){
                tx.rollback();
            }
        }
    }
    public void deleteUser(int id){
        Transaction tx = null;
        try {
            tx=userDao.currentSession().beginTransaction();
            User userToDelete = userDao.load(id);
            userDao.delete(userToDelete);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }
    }

    public User mergeUser(User user){
        Transaction tx = null;
        User persistentUser = null;

        try {
            tx=userDao.currentSession().beginTransaction();
            persistentUser = userDao.merge(user);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!= null)
                tx.rollback();
        }
        return persistentUser;
    }
    public void saveOrUpdateUser(User user){
        Transaction tx =null;

        try {
            tx=userDao.currentSession().beginTransaction();
            userDao.saveOrUpdate(user);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
    }

    public List<User> getAll(){
        Transaction tx = null;
        List<User> userList = null;
        try {
            tx=userDao.currentSession().beginTransaction();
            userList = userDao.getAll();
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
        return userList;
    }

    public List<User> findByPage(int pageNo,int pageSize){
        Transaction tx = null;
        List<User> userList = null;
        try {
            tx=userDao.currentSession().beginTransaction();
            userList = new UserDao().findByPage(pageNo,pageSize);
            tx.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
            if(tx!=null)
                tx.rollback();
        }
        return userList;
    }
}
